package com.zy.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by ly on 2018/2/9
 * describe：时间
 */
public class DateUtil {
    /**
     *
     * <b>Function：</b>匹配输入的时间字符串是否有效,返回对应的格式<br>
     * <b>Desc：</b>只支持yyyy-MM-dd HH:mm:ss和yyyy/MM/dd HH:mm:ss模式,否则会返回空<br>
     *
     *
     *
     * @param dateStr 时间字符串
     * @return
     */
    public static String matches(String dateStr){
        String formatStr = "";
        if(dateStr.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2}\\s+\\d{1,2}\\:\\d{1,2}\\:\\d{1,2}\\.\\d{1,3}"))
        {
            formatStr = "yyyy-MM-dd HH:mm:ss.SSS";
        }
        else if(dateStr.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2}\\s+\\d{1,2}\\:\\d{1,2}\\:\\d{1,2}"))
        {
            formatStr = "yyyy-MM-dd HH:mm:ss";
        }
        else if(dateStr.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2}\\s+\\d{1,2}\\:\\d{1,2}"))
        {
            formatStr = "yyyy-MM-dd HH:mm";
        }
        else if(dateStr.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2}\\s+\\d{1,2}"))
        {
            formatStr = "yyyy-MM-dd HH";
        }
        else if(dateStr.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2}"))
        {
            formatStr = "yyyy-MM-dd";
        }
        else if(dateStr.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2}\\s+\\d{1,2}\\:\\d{1,2}\\:\\d{1,2}\\.\\d{1,3}"))
        {
            formatStr = "yyyy/MM/dd HH:mm:ss.SSS";
        }
        else if(dateStr.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2}\\s+\\d{1,2}\\:\\d{1,2}\\:\\d{1,2}"))
        {
            formatStr = "yyyy/MM/dd HH:mm:ss";
        }
        else if(dateStr.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2}\\s+\\d{1,2}\\:\\d{1,2}"))
        {
            formatStr = "yyyy/MM/dd HH:mm";
        }
        else if(dateStr.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2}\\s+\\d{1,2}"))
        {
            formatStr = "yyyy/MM/dd HH";
        }
        else if(dateStr.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2}"))
        {
            formatStr = "yyyy/MM/dd";
        }
        return formatStr;
    }


    /**
     * 格式化时间戳为字符串
     * @param
     * @param formatStr 格式化如：yyyy-MM-dd HH:mm:ss
     *
     * @return String
     */
    public static String  format(long time, String formatStr){
        return format(new Date(time), formatStr);
    }


    /**
     * 格式化日期为字符串
     * @param date
     * @param formatStr 输出格式
     *
     * @return String
     */
    public static String  format(Date date, String formatStr){
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        String dateStr = sdf.format(date);
        return dateStr;
    }


    /**
     * 获取字符串格式日期的时间戳
     * @param dateStr  只支持格式:yyyy-MM-dd HH:mm:ss 或 yyyy/MM/dd HH:mm:ss
     *
     * @return 时间戳
     */
    public static long  getTimeInMillis(String dateStr){
        String formatStr = matches(dateStr);
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        long time = 0;
        try {
            time = sdf.parse(dateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }


    /**
     * 获取多少天前的时间戳
     * @param days    天数
     * @param hour    时 0-23
     * @param minute  分 0-59
     * @param second  秒 0-59
     *
     * @return 时间戳
     */
    public static long getTimeInMillis(int days, int hour, int minute,
                                       int second ){
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int date = calendar.get(Calendar.DAY_OF_MONTH) - days;
        calendar.set(year, month, date, hour, minute, second);
        long time = calendar.getTimeInMillis();
        time = time/1000L*1000L;   //最后3位毫秒数归零
        return time;
    }


    /**
     * <b>获取本周星期几的0点时间戳</b><br>
     *
     *
     *
     * @param week 如：Calendar.MONDAY 需要周几就传周几
     *
     * @return
     */
    public static long getWeekTime(int week){
        return getWeekTime(week, 0, 0, 0);
    }


    /**
     * <b>获取本周星期几的指定时间戳</b><br>
     *
     *
     *
     * @param week 如：Calendar.MONDAY 需要周几就传周几
     * @param hour    时 0-23
     * @param minute  分 0-59
     * @param second  秒 0-59
     *
     * @return
     */
    public static long getWeekTime(int week, int hour, int minute, int second){
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int date = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(year, month, date, hour, minute, second);
        calendar.set(Calendar.DAY_OF_WEEK, week);
        long time = calendar.getTimeInMillis();
        time = time/1000L*1000L;   //最后3位毫秒数归零
        return time;
    }


    /**
     * <b>获取当前年</b><br>
     *
     *
     *
     * @return
     */
    public static int getYear(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }


    /**
     * <b>获取当前月</b><br>
     * <b>
     * <b>D
     *
     * @return
     */
    public static int getMonth(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH)+1;
    }


    /**
     * <b>获取当前日</b><br>
     *
     *
     *
     * @return
     */
    public static int getDate(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_MONTH);
    }


    /**
     * <b>获取当前小时</b><br>
     *
     *
     *
     * @return
     */
    public static int getHour(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.HOUR_OF_DAY);
    }


    /**
     * <b>获取当前分钟</b><br>
     *
     *
     * @return
     */
    public static int getMinute(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MINUTE);
    }


    /**
     * <b>获取当前分钟</b><br>
     * <b>
     * <b>
     *
     * @return
     */
    public static int getSecond(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.SECOND);
    }

}
